
function solution(isBadVersion: (v: number) => boolean): (n: number) => number {
    return function(n: number) {
        var start = 1, end = n; 
        while (true) {
            var middle = Math.floor((end - start) / 2) + start;
            if (isBadVersion(middle)) {
                if (middle === 1 || !isBadVersion(middle - 1)) break;
                else {
                    end = middle - 1;
                }
            }
            else {
                start = middle + 1;
            }
            
        }
        return middle;
    };
};


var x = solution(n => n === 2)(5)

console.log("success !")